﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using LRSTaxServices.Biz;
using LRSTaxServices.Common;

public partial class Controls_SubAdminList : System.Web.UI.UserControl
{
    #region Types

    #region LayoutType

    public enum LayoutType
    {
        ListOnly,
        SearchAndList
    }

    #endregion

    public class SearchRequestedEventArgs : EventArgs
    {
        #region Construction / Disposal

        public SearchRequestedEventArgs() { }

        public SearchRequestedEventArgs(string firstName, string lastName, string userLogin, Schema.UserRoles? rbsRoleID)
        {
            FirstName = firstName;
            LastName = lastName;
            UserLogin = userLogin;
            RBSRoleID = rbsRoleID;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Search Parameter
        /// </summary>
        public string FirstName
        {
            get { return (_firstName); }
            set { _firstName = value; }
        } private string _firstName = null;


        /// <summary>
        /// Search Parameter
        /// </summary>
        public string LastName
        {
            get { return (_lastName); }
            set { _lastName = value; }
        } private string _lastName = null;


        /// <summary>
        /// Search Parameter
        /// </summary>
        public string UserLogin
        {
            get { return (_userLogin); }
            set { _userLogin = value; }
        } private string _userLogin = null;

        /// <summary>
        /// Search Parameter
        /// </summary>
        public Schema.UserRoles? RBSRoleID
        {
            get { return (_rbsRoleID); }
            set { _rbsRoleID = value; }
        } private Schema.UserRoles? _rbsRoleID = null;


        /// <summary>
        /// Search Parameter
        /// </summary>
        public bool? IsActive
        {
            get { return (_isActive); }
            set { _isActive = value; }
        } private bool? _isActive = null;


        /// <summary>
        /// Set to True iff the operation could not be completed. ErrorMessage property should contain the reason.
        /// </summary>
        public bool Cancel
        {
            get { return (_cancel); }
            set { _cancel = value; }
        } private bool _cancel = false;

        #endregion
    }

    public delegate void SearchRequestedEventHandler(object sender, SearchRequestedEventArgs evt);


    public class EditRequestedEventArgs : EventArgs
    {
        #region Construction / Disposal

        public EditRequestedEventArgs() { }

        public EditRequestedEventArgs(SubAdmin appUser)
        {
            User = appUser;
        }

        #endregion

        #region Public Properties

        public SubAdmin User
        {
            get { return (_appUser); }
            set { _appUser = value; }
        } private SubAdmin _appUser = null;

        #endregion
    }

    public delegate void EditRequestedEventHandler(object sender, EditRequestedEventArgs evt);

    #endregion

    #region Constants

    private const string PROP_LAYOUT_TYPE = "__UserListControl::LT";
    private const string PROP_ENABLE_ADDNEW = "__UserListControl::EAN";

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets / sets the object used as the data source for this control.
    /// </summary>
    public SubAdminCollection DataSource
    {
        get
        {
            if (_dataSource == null)
            {
                if (DataSourceRequested != null)
                    DataSourceRequested(this, EventArgs.Empty);
            }
            return (_dataSource);
        }
        set
        {
            if (_dataSource != value)
            {
                _dataSource = value;
                // OnDataSourceChanged();
            }
        }
    } private SubAdminCollection _dataSource = null;


    public LayoutType UILayout
    {
        get
        {
            if (ViewState[ClientID + PROP_LAYOUT_TYPE] == null)
                ViewState[ClientID + PROP_LAYOUT_TYPE] = LayoutType.ListOnly;

            return ((LayoutType)ViewState[ClientID + PROP_LAYOUT_TYPE]);
        }
        set
        {
            if (ViewState[ClientID + PROP_LAYOUT_TYPE] == null)
            {
                ViewState[ClientID + PROP_LAYOUT_TYPE] = (int)value;
                SetLayout(value);
            }
            else
            {
                LayoutType currentMode = ViewState[ClientID + PROP_LAYOUT_TYPE] is int ? (LayoutType)ViewState[ClientID + PROP_LAYOUT_TYPE] : LayoutType.ListOnly;

                if (currentMode != value)
                {
                    ViewState[ClientID + PROP_LAYOUT_TYPE] = value;
                    SetLayout(value);
                }
            }
        }
    }


    public bool EnableAddNew
    {
        get { return (ViewState[ClientID + PROP_ENABLE_ADDNEW] is bool ? (bool)ViewState[ClientID + PROP_ENABLE_ADDNEW] : true); }
        set { ViewState[ClientID + PROP_ENABLE_ADDNEW] = value; }
    }

    #endregion

    #region Public Events

    public event SearchRequestedEventHandler SearchRequested;

    public event EditRequestedEventHandler EditRequested;

    public event EventHandler AddNewRequested;

    public event EventHandler DataSourceRequested;

    #endregion

    #region Public Methods

    /// <summary>
    /// Method initializes and binds the controls UI elements.
    /// </summary>
    public bool DataBind(SubAdminCollection users, LayoutType uiLayoutType)
    {
        DataSource = users;
        UILayout = uiLayoutType;

        SetLayout(UILayout);

        GridSubAdminList.DataSource = DataSource;
        GridSubAdminList.DataBind();

        return (true);
    }


    /// <summary>
    /// Sets the selected row of the Users grid to the specified 'user'.
    /// </summary>
    public bool SetSelectedUser(SubAdmin user)
    {
        // TODO: Set Focus in the grid to the specified 'user'.
        return (true);
    }

    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #endregion

    #region Grid Events

    protected void GridSubAdminList_EditCommand(object source, DataGridCommandEventArgs evt)
    {
        DataGridItem item = evt.Item as DataGridItem;
        SubAdmin user = DataSource[item.DataSetIndex];

        //if (GridSubAdminList.EditItems.Count > 0)
        //    GridSubAdminList.MasterTableView.ClearEditItems();

        if (EditRequested != null)
            EditRequested(this, new EditRequestedEventArgs(user));
    }

    protected void GridSubAdminList_ItemCommand(object source, DataGridCommandEventArgs evt)
    {
        if (evt.CommandName.ToUpper() == "INSERT")
        {
            if (AddNewRequested != null)
                AddNewRequested(this, EventArgs.Empty);
        }
        else if (evt.CommandName.ToUpper() == "DELETE")
        {
        }
    }

    #endregion

    #endregion

    #region Privates

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.ListOnly:
                break;

            case LayoutType.SearchAndList:
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        SetInitialFocus(uiLayoutType);
    }

    #endregion

    #region SetInitialFocus

    protected void SetInitialFocus(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.ListOnly:
                break;

            case LayoutType.SearchAndList:
                break;
        }
    }

    #endregion

    #endregion
}
